﻿// ———————————————————————–
// <copyright file="ValueListBuilder.cs" company="The MITRE Corporation">
//    Copyright (c) 2010 The MITRE Corporation. All rights reserved.
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//    http://www.apache.org/licenses/LICENSE-2.0
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.
// </copyright>
// ———————————————————————–
/////////////////////////////////////////////////////////////////////////////////////////////////
// ValueListBuilder.cs - Library Class for the EDXL-DE Message
// Project: IC.NET - EDXL Library
//
// Language:    C#, .NET 4.0
// Platform:    Windows 7, VS 2010 Beta 2
// Author:      Joseph Whitmore, Bedford

/////////////////////////////////////////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EDXLSharp;

namespace EDXLSharp.EDXLTestApplication
{
  /// <summary>
  /// ValueList editting form
  /// </summary>
  public partial class MFormValueList : Form
  {
    /// <summary>
    /// Initializes a new instance of the MFormValueList class
    /// </summary>
    public MFormValueList()
    {
      this.InitializeComponent();
    }

    /// <summary>
    /// Load the specified ValueList into the ValueListBuilder form for editting
    /// </summary>
    /// <param name="vl">ValueList object to be editted</param>
    public void LoadValueList(ValueList vl)
    {
      if (vl != null)
      {
        this.mComboBoxURN.Items.Add(vl.ValueListURN);
        this.mComboBoxURN.SelectedIndex = 0;
        foreach (string str in vl.Value)
        {
          this.mListBoxValues.Items.Add(str);
        }
      }
    }

    /// <summary>
    /// Add a value to the ValueList builder value list box
    /// </summary>
    /// <param name="sender">Object generating the event</param>
    /// <param name="e">Event generated by the object</param>
    private void MButtonAddValue_Click(object sender, EventArgs e)
    {
      if (this.mComboBoxValue.SelectedIndex > 0)
      {
        this.mListBoxValues.Items.Add(this.mComboBoxValue.SelectedItem);
      }
      else if (this.mComboBoxValue.Text != null && this.mComboBoxValue.Text.Length > 0)
      {
        this.mListBoxValues.Items.Add(this.mComboBoxValue.Text);
      }
      else
      {
        this.toolStripMessage.Text = "Cannot add 'NULL' value.";
        this.toolStripStatusLabel1.Visible = true;
        this.toolStripMessage.Visible = true;
        return;
      }

      this.mComboBoxValue.Text = null;
      this.mComboBoxValue.SelectedIndex = -1;
      this.toolStripStatusLabel1.Visible = false;
      this.toolStripMessage.Visible = false;
      this.toolStripMessage.Text = null;
    }

    /// <summary>
    /// Remove the selected items from the a ListBox
    /// </summary>
    /// <param name="sender">Object generating the event</param>
    /// <param name="e">Event generated by the object</param>
    private void MButtonRemoveValues_Click(object sender, EventArgs e)
    {
      string res = Helper.MRemoveSelectedValuesFromListBox(this.mListBoxValues);
      if (res != null)
      {
        this.toolStripMessage.Text = res;
        this.toolStripStatusLabel1.Visible = true;
        this.toolStripMessage.Visible = true;
      }
      else
      {
        this.toolStripStatusLabel1.Visible = false;
        this.toolStripMessage.Visible = false;
        this.toolStripMessage.Text = null;
      }
    }

    /// <summary>
    /// Generate a ValueList Object and close this window
    /// </summary>
    /// <param name="sender">Object generating the event</param>
    /// <param name="e">Event generated by the object</param>
    private void MButtonCommitValueListBuilder_Click(object sender, EventArgs e)
    {
      if (this.mComboBoxURN.SelectedIndex > -1 || (this.mComboBoxURN.Text != null && this.mComboBoxURN.Text.Length > 0))
      {
        if (this.mListBoxValues.Items.Count > 0)
        {
          ValueList mVL = new ValueList();
          if (this.mComboBoxURN.SelectedIndex > -1)
          {
            mVL.ValueListURN = this.mComboBoxURN.SelectedItem.ToString();
          }
          else
          {
            mVL.ValueListURN = this.mComboBoxURN.Text;
          }

          foreach (object obj in this.mListBoxValues.Items)
          {
            if (mVL.Value == null)
            {
              mVL.Value = new List<string>();
            }

            mVL.Value.Add((string)obj);
          }

          this.mValueList = mVL;

          // close window
          this.toolStripStatusLabel1.Visible = false;
          this.toolStripMessage.Visible = false;
          this.toolStripMessage.Text = null;
          this.Close();
          return;
        }
        else
        {
          // no Vlaues
          this.toolStripMessage.Text = "No Values set";
          this.toolStripStatusLabel1.Visible = true;
          this.toolStripMessage.Visible = true;
        }
      }
      else
      {
        // no URN
        this.toolStripMessage.Text = "No URN Value set";
        this.toolStripStatusLabel1.Visible = true;
        this.toolStripMessage.Visible = true;
      }
    }

    /// <summary>
    /// Cancel changes and close the form window
    /// </summary>
    /// <param name="sender">Object generating the event</param>
    /// <param name="e">Event generated by the object</param>
    private void MButtonCancelValueListBuilder_Click(object sender, EventArgs e)
    {
      // clear and close form
      this.mComboBoxURN.Text = null;
      this.mComboBoxURN.SelectedIndex = -1;
      this.mComboBoxValue.Text = null;
      this.mComboBoxValue.SelectedIndex = -1;
      this.mListBoxValues.Items.Clear();
      this.mValueList = null;
      this.Close();
    }
  }
}
